<template>
  <transition 
    name="loading-fade"
    @enter="onEnter"
    @leave="onLeave"
  >
    <div v-if="visible" class="global-loading-overlay">
      <div class="loading-container">
        <!-- 主要加载动画 -->
        <div class="loading-animation">
          <div class="logo-container">
            <img src="../assets/logo.png" alt="Logo" class="loading-logo" />
            <div class="logo-glow"></div>
          </div>
          
          <!-- 动态圆环 -->
          <div class="loading-rings">
            <div class="ring ring-1"></div>
            <div class="ring ring-2"></div>
            <div class="ring ring-3"></div>
          </div>
          
          <!-- 进度条 -->
          <div class="progress-container">
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: `${progress}%` }"></div>
            </div>
            <div class="progress-text">{{ progressText }}</div>
          </div>
          
          <!-- 加载提示 -->
          <div class="loading-tips">
            <transition name="tip-fade" mode="out-in">
              <div :key="currentTip" class="tip-item">
                {{ currentTip }}
              </div>
            </transition>
          </div>
        </div>
        
        <!-- 背景粒子 -->
        <div class="loading-particles">
          <div 
            v-for="i in 20" 
            :key="i" 
            class="particle"
            :style="getParticleStyle(i)"
          ></div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
import { gsap } from 'gsap'

interface Props {
  visible?: boolean
  progress?: number
  text?: string
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  progress: 0,
  text: '加载中...',
})

// 加载提示语
const tips = [
  '正在准备精美界面...',
  '加载图片资源中...',
  '初始化动画效果...',
  '连接服务器...',
  '即将完成...',
]

const currentTipIndex = ref(0)
const currentTip = computed(() => tips[currentTipIndex.value])
const progressText = computed(() => props.text)

// 定时更换提示语
let tipInterval: number

const startTipRotation = () => {
  tipInterval = setInterval(() => {
    currentTipIndex.value = (currentTipIndex.value + 1) % tips.length
  }, 2000)
}

const stopTipRotation = () => {
  if (tipInterval) {
    clearInterval(tipInterval)
  }
}

// 粒子样式生成
const getParticleStyle = (index: number) => {
  const angle = (360 / 20) * index
  const radius = 200 + Math.random() * 100
  const x = Math.cos((angle * Math.PI) / 180) * radius
  const y = Math.sin((angle * Math.PI) / 180) * radius
  
  return {
    left: `calc(50% + ${x}px)`,
    top: `calc(50% + ${y}px)`,
    animationDelay: `${index * 0.1}s`,
    animationDuration: `${3 + Math.random() * 2}s`,
  }
}

// 动画控制
const onEnter = (el: Element) => {
  const timeline = gsap.timeline()
  
  timeline
    .from('.loading-logo', {
      scale: 0,
      rotation: -180,
      duration: 0.8,
      ease: 'back.out(1.7)',
    })
    .from('.ring', {
      scale: 0,
      opacity: 0,
      duration: 0.6,
      stagger: 0.1,
      ease: 'power2.out',
    }, '-=0.4')
    .from('.progress-container', {
      y: 30,
      opacity: 0,
      duration: 0.5,
      ease: 'power2.out',
    }, '-=0.3')
    .from('.loading-tips', {
      y: 20,
      opacity: 0,
      duration: 0.4,
      ease: 'power2.out',
    }, '-=0.2')
    .from('.particle', {
      scale: 0,
      opacity: 0,
      duration: 0.8,
      stagger: 0.05,
      ease: 'power2.out',
    }, '-=0.6')
}

const onLeave = (el: Element, done: () => void) => {
  const timeline = gsap.timeline({ onComplete: done })
  
  timeline
    .to('.particle', {
      scale: 0,
      opacity: 0,
      duration: 0.4,
      stagger: 0.02,
      ease: 'power2.in',
    })
    .to('.loading-tips, .progress-container', {
      y: -20,
      opacity: 0,
      duration: 0.3,
      ease: 'power2.in',
    }, '-=0.3')
    .to('.ring', {
      scale: 1.5,
      opacity: 0,
      duration: 0.4,
      stagger: 0.05,
      ease: 'power2.in',
    }, '-=0.2')
    .to('.loading-logo', {
      scale: 0,
      rotation: 180,
      duration: 0.5,
      ease: 'back.in(1.7)',
    }, '-=0.3')
}

// 监听可见性变化
watch(() => props.visible, (newVal) => {
  if (newVal) {
    startTipRotation()
  } else {
    stopTipRotation()
  }
})

onMounted(() => {
  if (props.visible) {
    startTipRotation()
  }
})

onUnmounted(() => {
  stopTipRotation()
})
</script>

<style scoped>
.global-loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, 
    rgba(15, 23, 42, 0.95), 
    rgba(30, 41, 59, 0.95)
  );
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-container {
  position: relative;
  text-align: center;
  max-width: 400px;
  width: 100%;
  padding: 2rem;
}

.loading-animation {
  position: relative;
  z-index: 2;
}

/* Logo 容器 */
.logo-container {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 2rem;
}

.loading-logo {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  filter: drop-shadow(0 0 20px rgba(14, 165, 233, 0.6));
}

.logo-glow {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100px;
  height: 100px;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle, rgba(14, 165, 233, 0.4), transparent 70%);
  border-radius: 50%;
  animation: pulse-glow 2s ease-in-out infinite;
}

/* 动态圆环 */
.loading-rings {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
}

.ring {
  position: absolute;
  border: 2px solid transparent;
  border-radius: 50%;
  animation: rotate 3s linear infinite;
}

.ring-1 {
  width: 100%;
  height: 100%;
  border-top: 2px solid #0ea5e9;
  border-right: 2px solid #0ea5e9;
  animation-duration: 2s;
}

.ring-2 {
  width: 80%;
  height: 80%;
  top: 10%;
  left: 10%;
  border-bottom: 2px solid #3b82f6;
  border-left: 2px solid #3b82f6;
  animation-duration: 1.5s;
  animation-direction: reverse;
}

.ring-3 {
  width: 60%;
  height: 60%;
  top: 20%;
  left: 20%;
  border-top: 2px solid #8b5cf6;
  border-bottom: 2px solid #8b5cf6;
  animation-duration: 1s;
}

/* 进度条 */
.progress-container {
  margin: 3rem 0 2rem;
}

.progress-bar {
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 1rem;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #0ea5e9, #3b82f6, #8b5cf6);
  border-radius: 2px;
  transition: width 0.3s ease;
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, 
    transparent 0%, 
    rgba(255, 255, 255, 0.4) 50%, 
    transparent 100%
  );
  animation: progress-shine 2s ease-in-out infinite;
}

.progress-text {
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.9rem;
  font-weight: 500;
}

/* 加载提示 */
.loading-tips {
  margin-top: 1.5rem;
}

.tip-item {
  color: rgba(255, 255, 255, 0.7);
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.5;
}

/* 背景粒子 */
.loading-particles {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 1;
}

.particle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: linear-gradient(45deg, #0ea5e9, #3b82f6);
  border-radius: 50%;
  animation: float-particle 4s ease-in-out infinite;
}

/* 过渡动画 */
.loading-fade-enter-active,
.loading-fade-leave-active {
  transition: opacity 0.5s ease;
}

.loading-fade-enter-from,
.loading-fade-leave-to {
  opacity: 0;
}

.tip-fade-enter-active,
.tip-fade-leave-active {
  transition: all 0.3s ease;
}

.tip-fade-enter-from {
  opacity: 0;
  transform: translateY(10px);
}

.tip-fade-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

/* 动画关键帧 */
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes pulse-glow {
  0%, 100% {
    opacity: 0.6;
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
  }
}

@keyframes progress-shine {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@keyframes float-particle {
  0%, 100% {
    transform: translateY(0px) scale(1);
    opacity: 0.7;
  }
  50% {
    transform: translateY(-20px) scale(1.2);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .loading-container {
    padding: 1rem;
    max-width: 300px;
  }
  
  .logo-container {
    width: 100px;
    height: 100px;
    margin-bottom: 1.5rem;
  }
  
  .loading-logo {
    width: 60px;
    height: 60px;
  }
  
  .loading-rings {
    width: 100px;
    height: 100px;
  }
  
  .progress-container {
    margin: 2rem 0 1.5rem;
  }
  
  .tip-item {
    font-size: 0.8rem;
  }
}
</style>
